<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="12">
          <a-form-item label="付款账号" v-bind="validateInfos.recAccount">
            <a-input v-model:value="formData.recAccount" placeholder="请输入付款账号" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="付款户名" v-bind="validateInfos.recAccountName">
            <a-input v-model:value="formData.recAccountName" placeholder="请输入付款户名" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="交易时间" v-bind="validateInfos.transferDate">
            <a-input v-model:value="formData.transferDate" placeholder="请输入交易时间" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="币种汉字" v-bind="validateInfos.currencyTypeHz">
            <a-input v-model:value="formData.currencyTypeHz" placeholder="请输入币种汉字" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="借方金额" v-bind="validateInfos.debitAmount">
            <a-input v-model:value="formData.debitAmount" placeholder="请输入借方金额" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="贷方金额" v-bind="validateInfos.loanamount">
	          <a-input-number v-model:value="formData.loanamount" placeholder="请输入贷方金额" style="width: 100%" disabled/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="摘要码" v-bind="validateInfos.summaryNo">
            <a-input v-model:value="formData.summaryNo" placeholder="请输入摘要码" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="摘要" v-bind="validateInfos.summary">
            <a-input v-model:value="formData.summary" placeholder="请输入摘要" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="打款备注" v-bind="validateInfos.payRem">
            <a-input v-model:value="formData.payRem" placeholder="请打款备注" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="余额" v-bind="validateInfos.balance">
	          <a-input-number v-model:value="formData.balance" placeholder="请输入余额" style="width: 100%" disabled/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="币种" v-bind="validateInfos.currencyType">
            <a-input v-model:value="formData.currencyType" placeholder="请输入币种" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="凭证号" v-bind="validateInfos.vouchersNo">
            <a-input v-model:value="formData.vouchersNo" placeholder="请输入凭证号" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="凭证种类" v-bind="validateInfos.vouchType">
            <a-input v-model:value="formData.vouchType" placeholder="请输入凭证种类" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="手续费" v-bind="validateInfos.tranFee">
	          <a-input-number v-model:value="formData.tranFee" placeholder="请输入手续费" style="width: 100%" disabled/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="明细序列号" v-bind="validateInfos.serialNo">
	          <a-input-number v-model:value="formData.serialNo" placeholder="请输入明细序列号" style="width: 100%" disabled/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="匹配状态" v-bind="validateInfos.matchState">
	          <j-dict-select-tag v-model:value="formData.matchState" dictCode="ZJPPZT" placeholder="请选择匹配状态" disabled/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="业务类型" v-bind="validateInfos.matchBusType">
	          <j-dict-select-tag v-model:value="formData.matchBusType" dictCode="PPYWLX" placeholder="请选择业务类型" disabled/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="订单编号" v-bind="validateInfos.matchBusSerialNo">
            <a-input v-model:value="formData.matchBusSerialNo" placeholder="请输入订单编号" disabled></a-input>
          </a-form-item>

        </a-col>
        <a-col :span="12">
          <a-form-item label="指令编号" v-bind="validateInfos.orderFlowNo">
            <a-input v-model:value="formData.orderFlowNo" placeholder="请输入指令编号" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="对手方银行名" v-bind="validateInfos.partyBankName">
            <a-input v-model:value="formData.partyBankName" placeholder="请输入对手方银行名" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="修正后的摘要码" v-bind="validateInfos.reviseSummaryNo">
            <a-input v-model:value="formData.reviseSummaryNo" placeholder="请输入修正后的摘要码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../InsuranceHangzhouBack.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    recAccount: '',   
    recAccountName: '',   
    transferDate: '',   
    currencyTypeHz: '',   
    debitAmount: '',   
    loanamount: undefined,
    summaryNo: '',   
    summary: '',
    payRem: '',
    balance: undefined,
    currencyType: '',   
    vouchersNo: '',   
    vouchType: '',   
    tranFee: undefined,
    serialNo: '',
    matchState: '',   
    matchBusType: '',   
    matchBusSerialNo: '',   
    partyBankName: '',   
    reviseSummaryNo: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    recAccount: [{ required: true, message: '请输入付款账号!'},],
    recAccountName: [{ required: true, message: '请输入付款户名!'},],
    serialNo: [{ required: true, message: '请输入明细序列号!'},],
    matchState: [{ required: true, message: '请输入匹配状态!'},],
    partyBankName: [{ required: true, message: '请输入对手方银行名!'},],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    min-height: 500px !important;
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }
</style>
